/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-01-30
 * Time:22:49
 */
public class MySingleList implements IMySingleList {

    static class ListNode {
        public int val;
        public ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
/////////////////////////////////////////////

    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            ++count;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");

            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            prev.next = null;
            prev = cur;
            cur = cur.next;
        }
        head = null;
    }
///////////////////////////////////////////////////
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (this.head == null) {
            this.head = listNode;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = listNode;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //判断插入下标是否合法
        if(index < 0 || index > this.size()) {
            throw new illegalIndexException("下标非法，无法插入");
        }
        //index为0，就是头插了
        if(index == 0) {
            addFirst(data);
            return;
        }

        int count = 0;
        ListNode node = new ListNode(data);
        ListNode prev = head;

        while(count < index-1) {
            prev = prev.next;
            ++count;
        }

        //此时prev指向下标为index的结点的前一个结点
        node.next = prev.next;
        prev.next = node;
    }
////////////////////////////////////////////////////////

    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

////////////////////////////////////////////////////////

    private ListNode findPrev(int key) {
        if(head == null) {
            return null;
        }
        ListNode prev = this.head;
        ListNode cur = prev.next;

        while(cur!=null) {
            if(cur.val == key) {
                return prev;
            }
            prev = cur;
            cur = cur.next;
        }
        return null;  //找不到就返回null
    }

    public void remove(int key) {
        if(head == null) {
            throw new ListIsEmptyException("单链表为空，不可删除！");
        }
        //如果头结点就是待删除的结点，那直接让下一个结点成为head
        if(head.val == key) {
            head = head.next;
        }
        ListNode prev = findPrev(key);
        if(prev == null) {
            return;
        } else {
            ListNode del = prev.next;
            prev.next = del.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = prev.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }

}
